package com.ruoyi.web.controller.project;

import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.domain.Material;
import com.ruoyi.system.service.IMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProjectMaterial;
import com.ruoyi.system.service.IProjectMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目材料Controller
 * 
 * @author ruoyi
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/project/material")
@RequiredArgsConstructor
@Api(tags = "项目材料")
public class ProjectMaterialController extends BaseController{

    private final IProjectMaterialService projectMaterialService;

    private final IMaterialService materialService;


    @GetMapping("/list")
    @ApiOperation("查询项目材料列表")
    public TableDataInfo list(@RequestParam("projectId")Integer projectId)
    {
        startPage();
        QueryWrapper<ProjectMaterial> wrapper = new QueryWrapper<>();
        wrapper.select("*,(select sum(number) from project_purchase_material where  project_purchase_material.project_material_id = project_material.id ) as alreadyNumber");
        wrapper.eq("project_id",projectId);
        List<ProjectMaterial> list = projectMaterialService.list(wrapper);
        return getDataTable(list);
    }



    @GetMapping(value = "/{id}")
    @ApiOperation("获取项目材料详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(projectMaterialService.getById(id));
    }


    @Log(title = "项目材料", businessType = BusinessType.INSERT)
    @ApiOperation("新增项目材料")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProjectMaterial projectMaterial)
    {
        Material material = new Material();
        material.setName(projectMaterial.getName());
        material.setSpecification(projectMaterial.getSpecification());
        material.setUnit(projectMaterial.getUnit());
        materialService.saveMaterial(material);
        return toAjax(projectMaterialService.save(projectMaterial));
    }


    @Log(title = "项目材料", businessType = BusinessType.UPDATE)
    @ApiOperation("修改项目材料")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProjectMaterial projectMaterial)
    {
        Material material = new Material();
        material.setName(projectMaterial.getName());
        material.setSpecification(projectMaterial.getSpecification());
        material.setUnit(projectMaterial.getUnit());
        materialService.saveMaterial(material);
        return toAjax(projectMaterialService.updateById(projectMaterial));
    }


    @Log(title = "项目材料", businessType = BusinessType.DELETE)
    @ApiOperation("删除项目材料")
	@PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(projectMaterialService.removeBatchByIds(Arrays.asList(ids)));
    }
}
